/**
 * Handle changed data.
 *
 * @param {Number} oldValue old value
 * @param {Number} newValue new value
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"36996308-9428-4D23-B19E-063CF9889074"}
 */
function onDataChangeEnteros(oldValue, newValue, event) {
	if (newValue < 0)
	{
		globals.DIALOGS.showErrorDialog('Error','El numero debe estar entre 0 y 99','OK')
		return false
	}
	return true
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"E7A88603-616B-4BC6-B209-C8119BA3FF36"}
 */
function agregar(event) {
	var $rel = elements.Hoteles.getTabRelationNameAt(elements.Hoteles.tabIndex)
	forms.carga_reserva[$rel].newRecord()
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"04C8DD86-8C35-4520-9153-E9AF0D33278A"}
 */
function eliminar_reserva(event) {
	reservacab_to_reservahtl.reservahtl_to_reservahab.deleteAllRecords()
	reservacab_to_reservahtl.deleteAllRecords()
	reservacab_to_reservatra.reservatra_to_reservatrf.deleteAllRecords()
	reservacab_to_reservatra.deleteAllRecords()
	reservacab_to_reservaexc.reservaexc_to_reservajor.deleteAllRecords()
	reservacab_to_reservaexc.deleteAllRecords()
	controller.deleteRecord()
	forms.menu.controller.show()
}

/**
 * Perform the element default action.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"EBE409B3-B6C3-40E4-ABE0-148DAE6AB5E9"}
 * @SuppressWarnings(unused)
 */
function confirmar_reserva(event) {
	if (rc_nombre_pax == '')
	{
		globals.DIALOGS.showErrorDialog('Error','El nombre del pasajero es obligatorio','OK')
		return
	}
	if (rc_qmayores == null)
	{
		globals.DIALOGS.showErrorDialog('Error','La cantidad de pasajeros es obligatoria','OK')
		return
	}

	if (rc_operador_id == null)
	{
		globals.DIALOGS.showErrorDialog('Error','Debe ingresar Operador','OK')
		return
	}	
	
	var $cant = rc_qmayores + rc_qmenores
	
	if (reservacab_to_reservapsj.getSize() != $cant)
	{
		globals.DIALOGS.showErrorDialog('Error','Faltan datos de pasajeros','OK')
		return
	}
	
	var $tot_my = 0
	var $tot_mn = 0
	for (var i = 1 ; i <= reservacab_to_reservahtl.getSize() ; i++)
	{
		var $htl = reservacab_to_reservahtl.getRecord(i)
		for (var j = 1 ; j <= $htl.reservahtl_to_reservahab.getSize() ; j++)
		{
			var $hab = $htl.reservahtl_to_reservahab.getRecord(j)
			
			$tot_my += $hab.rb_qty_my
			$tot_mn += $hab.rb_qty_mn
		}
	}
	
	//if (rc_qmayores != $tot_my)
	//{
		//globals.DIALOGS.showErrorDialog('Error','Cantidad de mayores distinta a la cantidad de reservas','OK')
		//return
	//}
	//if (rc_qmenores != $tot_mn && rc_qmenores != null)
	//{
		//globals.DIALOGS.showErrorDialog('Error','Cantidad de menores distinta a la cantidad de reservas','OK')
		//return
	//}
	
	if (rc_posicion == null || rc_posicion == 0)
	{
		var $ultimo = buscarUltimo()
		if ($ultimo == -1)
		{
			globals.DIALOGS.showErrorDialog('Error','No se puede numerar la reserva','OK')
			return
		}
		rc_posicion = $ultimo
		grabarFicha()
	}
	
	if (databaseManager.saveData() == false)
	{
		globals.DIALOGS.showErrorDialog('Error','Ocurrio un error al grabar','OK')
		return
	}
	else
	{		
		globals.DIALOGS.showInfoDialog('Exito','Solicitud en Proceso','OK')
		forms.main.controller.show()
        forms.menu.elements.grp_btnes.visible = true
		return
	}
}

/**
 * @properties={typeid:24,uuid:"BABF253A-67D8-4F51-8DFC-6565C7D66EB5"}
 * @AllowToRunInFind
 */
function buscarUltimo()
{
	/** @type {JSFoundSet<db:/sytek/ultimos>}*/
	var $ultimos = databaseManager.getFoundSet('sytek','ultimos')
	
	$ultimos.find()
	
	$ultimos.kabc = ' '
	$ultimos.kptovta = 0
	$ultimos.kult = 'LEF'
		
	if ($ultimos.search() == 0)
	{
		return -1
	}
	$ultimos.numult_2++
	return $ultimos.numult_2
}

/**
 * @properties={typeid:24,uuid:"100771B0-26E4-439E-8305-FB3228D132D5"}
 */
function grabarFicha()
{
	/** @type {JSFoundSet<db:/sytek/fichas>}*/
	var $fichas = databaseManager.getFoundSet('sytek','fichas')
	
	$fichas.newRecord()
	$fichas.kleg = rc_posicion
	$fichas.fichnom = rc_nombre_pax
	$fichas.fichdeu = rc_operador_id
	$fichas.leg_aa = 0
	$fichas.leg_dd = 0
	$fichas.leg_mm = 0
	$fichas.tipo_reserva = 'T'
}

/**
 * Callback method for when form is shown.
 *
 * @param {Boolean} firstShow form is shown first time after load
 * @param {JSEvent} event the event that triggered the action
 *
 * @properties={typeid:24,uuid:"CA4E91F1-1DB6-402B-A6EF-98907CB811F9"}
 */
function onShow(firstShow, event) {
	databaseManager.setAutoSave(false)
	controller.newRecord()
}

/**
 * Handle hide window.
 *
 * @param {JSEvent} event the event that triggered the action
 *
 * @returns {Boolean}
 *
 * @properties={typeid:24,uuid:"52438F67-51B0-4AAE-86BC-AF86BA4D8102"}
 */
function onHide(event) {
	databaseManager.revertEditedRecords()
	databaseManager.setAutoSave(true)
	return true
}
